package password

import (
	"golang.org/x/crypto/bcrypt"
)

// func Encrypt(c *gin.Context) {

// }

// password encrypt by bcrypt
func Encrypt(password string) (string, error) {
	bytes, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
	return string(bytes), err
}

func isEncrypted(password string) bool {
	// bcrypt.Cost returns the hashing cost used to create the given hashed
	cost, _ := bcrypt.Cost([]byte(password))
	// cost > 0 means the password has been encrypted
	return cost > 0
}

func PasswordVerify(encryptedPassword, password string) bool {
	if err := bcrypt.CompareHashAndPassword([]byte(encryptedPassword), []byte(password)); err != nil {
		return false
	}
	return true
}
